package Lab1;
import java.awt.Color;

import java.util.ArrayList;



import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.data.xy.XYDataset;

import org.jfree.data.xy.XYSeries;

import org.jfree.data.xy.XYSeriesCollection;

import org.jfree.ui.ApplicationFrame;

import org.jfree.ui.RefineryUtilities;





public class GerarGrafico extends ApplicationFrame {



	private static ArrayList <Integer> resultado;

	private static int qtdamostral;

	private static final long serialVersionUID = 1L;



    public GerarGrafico(final String title, ArrayList<Integer> result, int qtdamostral) {



        super(title);

        this.qtdamostral = qtdamostral;

        this.resultado = result;

        final XYDataset dataset = createDataset();

        final JFreeChart chart = createChart(dataset);

        final ChartPanel chartPanel = new ChartPanel(chart);

        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

        setContentPane(chartPanel);



    }

    

    private XYDataset createDataset() {

        

        final XYSeries series1 = new XYSeries("First");

        for (int i = 0; i < resultado.size(); i++) {

        	series1.add(i,resultado.get(i));	

		}

        



        final XYSeriesCollection dataset = new XYSeriesCollection();

        dataset.addSeries(series1);

        return dataset;

        

    }

   

    private JFreeChart createChart(final XYDataset dataset) {

        

        // create the chart...

        final JFreeChart chart = ChartFactory.createXYLineChart(

            "Grafico Selection Sort",      // chart title

            "X",                      // x axis label

            "Y",                      // y axis label

            dataset,                  // data

            PlotOrientation.VERTICAL,

            true,                     // include legend

            true,                     // tooltips

            false                     // urls

        );



        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        chart.setBackgroundPaint(Color.white);



//        final StandardLegend legend = (StandardLegend) chart.getLegend();

  //      legend.setDisplaySeriesShapes(true);

        

        // get a reference to the plot for further customisation...

        final XYPlot plot = chart.getXYPlot();

        plot.setBackgroundPaint(Color.lightGray);

    //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

        plot.setDomainGridlinePaint(Color.white);

        plot.setRangeGridlinePaint(Color.white);

        

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

        renderer.setSeriesLinesVisible(0, false);

        renderer.setSeriesShapesVisible(1, false);

        plot.setRenderer(renderer);



        // change the auto tick unit selection to integer units only...

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // OPTIONAL CUSTOMISATION COMPLETED.

                

        return chart;

        

    }



    // ****************************************************************************

    // * JFREECHART DEVELOPER GUIDE                                               *

    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *

    // * to purchase from Object Refinery Limited:                                *

    // *                                                                          *

    // * http://www.object-refinery.com/jfreechart/guide.html                     *

    // *                                                                          *

    // * Sales are used to provide funding for the JFreeChart project - please    * 

    // * support us so that we can continue developing free software.             *

    // ****************************************************************************

    

    /**

     * Starting point for the demonstration application.

     *

     * @param args  ignored.

     */

    public static void main(final String[] args) {



        final GerarGrafico demo = new GerarGrafico("Selection Sort", resultado, qtdamostral);

        demo.pack();

        RefineryUtilities.centerFrameOnScreen(demo);

        demo.setVisible(true);



    }



}

